package cn.edu.scau.cmi.liangzaoqing.controller.rest;

import cn.edu.scau.cmi.liangzaoqing.domain.UserTest;
import cn.edu.scau.cmi.liangzaoqing.repository.UserTestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserTestRestController {
    @Autowired
    UserTestRepository userTestRepository;

    @RequestMapping(value ="/restRegister", method = RequestMethod.GET)
    public String register(Model model, UserTest userTest) {
        model.addAttribute("userTest", userTest);
        return "register";
    }

    @RequestMapping(value ="/restNewUserTest")
    public String newUser(Model model, UserTest userTest) {
        model.addAttribute("userTest", userTest);
        UserTest isNewUserTest = userTestRepository.findUserByName(userTest.getName());
        //判断该用户名是否被注册过
        if (isNewUserTest == null) {
            userTestRepository.save(userTest);
            return "redirect:register?success";
        } else {
            return "redirect:register?error";
        }
    }

    @RequestMapping(value ="/restListUserTest")
    public void listUsers() {
        UserTest user = userTestRepository.findUserByName("zhangsan");
        System.out.println(user.getName() + user.getId());

    }



}
